﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BUS;
using System.IO;
using TuyenDungViecLam.CommonResource;

namespace TuyenDungViecLam.NhaTuyenDung
{
    public partial class XemCVUpLoad : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LienKetDuLieu();
            }
        }

        private void LienKetDuLieu()
        {
            //int userID = int.Parse(Session["UserID"].ToString());
            int userID = 13;

            IEnumerable ds = NhaTuyenDung_BUS.LayCVUpload(userID);


            GrvCVUpload.DataSource = NhaTuyenDung_BUS.LayCVUpload(userID);
            GrvCVUpload.DataBind();

            if (GrvCVUpload.Rows.Count == 0)
                LbThongBao.Text = "Không có CV upload nào được gửi";
        }

        protected void GrvCVUpload_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Download")
            {
                try
                {
                    string filePath = e.CommandArgument.ToString();

                    int vt = filePath.LastIndexOf('/');
                    string fileName = filePath.Substring(vt + 1, filePath.Length - vt - 1);

                    //tien hanh cho download
                    string filePathOnServer = Server.MapPath("~/Upload/") + fileName;
                    FileInfo fileInfo = new FileInfo(filePathOnServer);

                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.WriteFile(filePathOnServer);
                    Response.End();
                }
                catch (Exception)
                {
                    Alert.Show("File này không tồn tại !!!");
                }
            }
        }

        protected void GrvCVUpload_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GrvCVUpload.PageIndex = e.NewPageIndex;
            LienKetDuLieu();
        }
    }
}
